#include "localmusicwidget.h"

LocalMusicWidget::LocalMusicWidget(QWidget*parent):AlbumWidget(parent)
{
    topw =new QWidget(this);
    localname=new QLabel("本地音乐",topw);
    localsongnum=new QLabel(topw);
    localfilebtn=new QPushButton("选择目录 >",topw);
    localtoplayout=new QHBoxLayout(topw);

    localtoplayout->addWidget(localname);
    localtoplayout->addWidget(localsongnum);
    localtoplayout->addStretch(1);
    localtoplayout->addWidget(localfilebtn);
    localname->setFont(QFont("Microsoft YaHei",16));
    localsongnum->setFont(QFont("Microsoft YaHei",8));
    localfilebtn->setFont(QFont("Microsoft YaHei",11));
    localfilebtn->setStyleSheet("QPushButton{"
                                "color:blue;"
                                "}");
    localfilebtn->setFixedWidth(100);
    localsongnum->setStyleSheet("QLabel{"
                                "color:grey;"
                                "}");



    connect(localfilebtn,&QPushButton::clicked,this,&LocalMusicWidget::onClickLocalFileBtn);

    //继承AlbumWidget，并把上面的专辑图片等控件移除,换成该控件
    mainlayout->removeWidget(albumtopw);
    albumtopw->hide();
    mainlayout->insertWidget(0,topw);
    topw->show();
    songitemfirst->SetLikeMode(false);

}

void LocalMusicWidget::ScanLocalDir(const QString&filepath)
{
    QVector<SongItem*>songitems;
    QDir dir(filepath);
    QFileInfoList entries = dir.entryInfoList(QDir::Files);
    for(const QFileInfo &entry : entries)
    {
        if(entry.suffix().toLower()=="mp3")//是mp3文件
        {
            auto ptr=SongItem::FromLocalFile(entry.absoluteFilePath());
            ptr->SetLikeMode(false);
            songitems.push_back(ptr);
        }
    }
    localsongnum->setText(QString("共%1首").arg(songitems.size()));
    AlbumWidget::AddSongItem(songitems);
}

void LocalMusicWidget::onClickLocalFileBtn()
{
    QString dirpath=QFileDialog::getExistingDirectory();
    qDebug()<<dirpath;
    ScanLocalDir(dirpath);
}
